package presentation;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.EmpresaPrivada;
import domain.Presupuesto;

/**
 * Servlet implementation class for Servlet: InsertarPresupuesto
 * 
 */
public class InsertarPresupuesto extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public InsertarPresupuesto() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesion = request.getSession();
		int idprivada = (Integer) sesion.getAttribute("session.id");
		Presupuesto p = new Presupuesto();
		p.setEprId(idprivada);
		p.setPId(Integer.parseInt(request.getParameter("idproyecto")));
		p.setNombre(request.getParameter("name"));
		p.setDescripcion(request.getParameter("descripcion"));
		p.setVentajas(request.getParameter("ventajas"));
		EmpresaPrivada EP = new EmpresaPrivada();
		System.out.println(p.getEprId());
		System.out.println(p.getPId());
		String resource = "./error/error.jsp";
		if (EP.insertPresupuesto(p)) {
			resource = "./EmpresaPrivada/entrada.jsp";
			RequestDispatcher d = request.getRequestDispatcher(resource);
			if (d != null) {
				d.forward(request, response);
			}
		} else {
			response.sendRedirect(resource);
		}

	}
}
